package client;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import service.ServiceHebergementDao;
import service.ServiceLieuDao;
import service.ServiceLieuDaoBean;
import service.ServiceModeTransportDao;
import service.ServiceTrajetDao;
import service.ServiceVoyageDao;
import dao.DaoFactory;
import dao.FactoryAbstractDaoImpl;
import dao.LieuDao;
import dao.LieuDaoJpa;
import dto.HebergementVO;
import dto.LieuVO;
import dto.ModeTransportVO;
import dto.TrajetVO;
import dto.VoyageVO;

public class TestServiceDao {

	private String[] tabProj = { "org.bovoyage" };
	private String interFace = "local";
//	private String interFace = "remote";
//	private String interFace = "create";
	private Properties props;
	private InitialContext ic;

	public static void main(String[] args) {
		(new TestServiceDao()).testServicesDao();
	}
	
	public void testServicesDao() {

		props = new Properties();
		InputStream lectureProps = TestServiceDao.class
				.getResourceAsStream("../client_jndi.properties");
		try {
			props.load(lectureProps);
			System.out.println("Fichier de configuration chargé");
		} catch (IOException e1) {
			e1.printStackTrace();
			return;
		}
		try {
			ic = new InitialContext(props);
		} catch (NamingException e) {
			e.printStackTrace();
		}
		System.out.println("InitialContext créé");
		
		testServiceLieuDao();
//		testServiceModeTransportDao();
//		testServiceTrajetDao();
//		testServiceHebergementDao();
//		testServiceVoyageDao();
	}

	private void testServiceLieuDao() {

		System.out.println("TEST ServiceLieuDao - debut");
		ServiceLieuDao serviceDao = null;
		if (interFace.equals("create")) {
			serviceDao = new ServiceLieuDaoBean();
			System.out.println("serviceDao créé");
//			LieuDao dao = new LieuDaoJpa();
			DaoFactory daoFactory = FactoryAbstractDaoImpl.getFactory("JPA");
			LieuDao dao = daoFactory.getLieuDao();
			System.out.println("dao créé");
			((ServiceLieuDaoBean) serviceDao).setLieuDao(dao);
		} else {
			for (String n : tabProj) {
				try {
					serviceDao = (ServiceLieuDao) ic.lookup(n
							+ "/ServiceLieuDaoBean/" + interFace);
					break;
				} catch (Exception e) {
					System.err.println("Lookup " + n + " KO");
//					System.err.println(e);
//					System.err.println(e.getStackTrace()[e.getStackTrace().length-1]);
					e.printStackTrace();
				}
			}
			if (serviceDao == null) {
				System.err.println("Lookup KO");
				return;
			}
			System.out.println("Lookup effectué");
		}

		System.out.println("auteur : " + serviceDao.getAuteur());

		for (LieuVO l : serviceDao.getAllLieux())
			System.out.println(l);
		System.out.println("TEST ServiceLieuDao - fin");
		System.out.println();
	}
	
	private void testServiceModeTransportDao() {

		System.out.println("TEST ServiceModeTransportDao - debut");
		ServiceModeTransportDao serviceDao = null;
		for (String n : tabProj) {
			try {
				serviceDao = (ServiceModeTransportDao) ic.lookup(n
						+ "/ServiceModeTransportDaoBean/local");
				System.out.println(serviceDao);
				break;
			} catch (Exception e) {
				System.err.println("Lookup " + n + " KO");
				System.err.println(e);
			}
		}
		if (serviceDao == null) {
			System.err.println("Lookup KO");
			return;
		}
		System.out.println("Lookup effectué");

		for (ModeTransportVO l : serviceDao.getAllModeTransports())
			System.out.println(l);
		System.out.println("TEST ServiceModeTransportDao - fin");
		System.out.println();
	}

	private void testServiceTrajetDao() {

		System.out.println("TEST ServiceTrajetDao - debut");
		ServiceTrajetDao serviceDao = null;
		for (String n : tabProj) {
			try {
				serviceDao = (ServiceTrajetDao) ic.lookup(n
						+ "/ServiceTrajetDaoBean/local");
				System.out.println(serviceDao);
				break;
			} catch (Exception e) {
				System.err.println("Lookup " + n + " KO");
				System.err.println(e);
			}
		}
		if (serviceDao == null) {
			System.err.println("Lookup KO");
			return;
		}
		System.out.println("Lookup effectué");

		for (TrajetVO t : serviceDao.getAllTrajets())
			System.out.println(t);
		System.out.println("TEST ServiceTrajetDao - fin");
		System.out.println();
	}

	private void testServiceHebergementDao() {

		System.out.println("TEST ServiceHebergementDao - debut");
		ServiceHebergementDao serviceDao = null;
		for (String n : tabProj) {
			try {
				serviceDao = (ServiceHebergementDao) ic.lookup(n
						+ "/ServiceHebergementDaoBean/local");
				System.out.println(serviceDao);
				break;
			} catch (Exception e) {
				System.err.println("Lookup " + n + " KO");
				System.err.println(e);
			}
		}
		if (serviceDao == null) {
			System.err.println("Lookup KO");
			return;
		}
		System.out.println("Lookup effectué");

		for (HebergementVO t : serviceDao.getAllHebergements())
			System.out.println(t);
		System.out.println("TEST ServiceHebergementDao - fin");
		System.out.println();
	}
	
	private void testServiceVoyageDao() {

		System.out.println("TEST ServiceVoyageDao - debut");
		ServiceVoyageDao serviceDao = null;
		for (String n : tabProj) {
			try {
				serviceDao = (ServiceVoyageDao) ic.lookup(n
						+ "/ServiceVoyageDaoBean/local");
				System.out.println(serviceDao);
				break;
			} catch (Exception e) {
				System.err.println("Lookup " + n + " KO");
				System.err.println(e);
			}
		}
		if (serviceDao == null) {
			System.err.println("Lookup KO");
			return;
		}
		System.out.println("Lookup effectué");

		for (VoyageVO v : serviceDao.getAllVoyages())
			System.out.println(v);
		System.out.println("TEST ServiceVoyageDao - fin");
		System.out.println();
	}
}